<template lang="pug">
dialogModel(
    ref="dialogLoad"
    title="收藏夹"
    width="1280"
    :footer="false"
)
    .cont-wrap
        .search-w
            searchModel(:searchList="searchList" @searchSubmit="searchSubmit" @searchReset="searchReset()")
                template(#searchSlot)
                    el-button(type="primary" @click="clearFavorites()" style="margin-left: 616px" link) 清空收藏夹
        .table-w
            tableModel(
                ref="tableRef"
                :data="tableData"
                :pagination="false"
            )
                el-table-column(prop="fileName" label="文件名称" align="center" width="400" show-overflow-tooltip)
                el-table-column(prop="versionNum" label="版本号" align="center"  show-overflow-tooltip)
                    template(#default="{row}")
                        span V{{ row.versionNum }}
                el-table-column(prop="fileStatus" label="文件状态" align="center" show-overflow-tooltip)
                    template(#default="{row}")
                        span(:style="{color: row.fileStatus == 1 ? '' : 'red'}") {{ row.fileStatus == 1 ? '正常使用' : '作废' }}
                el-table-column(prop="parseStatus" label="解析状态" align="center" show-overflow-tooltip)
                    template(#default="{row}")
                        span(style="color:red" v-if='row.parseStatus == 0') 解析失败
                        span(v-else) {{ row.parseStatus == 1 ? '解析中' : '解析完成' }}
                el-table-column(prop="createDate" label="上传时间" align="center"  width="180" show-overflow-tooltip)
                el-table-column(label="操作" align="center" width="200px")
                    template(#default="{row}")
                        el-button(link type="primary" size="small" @click.stop="showFile(row)"  ) 预览
                        el-button(link type="primary" size="small" @click.stop="cyFiledownloadById(row)"  ) 下载
                el-table-column(label="收藏" align="center" width="60")
                    template(#default="{row}" )
                        el-icon(size="16" style="cursor: pointer;" @click="cyFilesaveOne(row)")
                            StarFilled( :title="row.collectStatus == 1 ? '以收藏' : '收藏'" :style="{color:row.collectStatus == 1 ? '#fa9734' : '#ccc'}" )
fileView(ref="fileViewRef")
</template>

<script setup>
import searchModel from '@/components/searchModel.vue'
import { ref, onMounted } from 'vue'
import tableModel from '@/components/tableModel.vue'
import dialogModel from '@/components/dialogModel.vue'
import apis from '@/apis'
import { success, confirm } from '@/utils/message'
import fileView from '@/components/fileView.vue'//预览
import { useRouter, useRoute } from 'vue-router'
const route = useRoute()

const dialogLoad = ref(null)
const open = () =>{
    dialogLoad.value.open()
    cyFilequeryAllSplit()
}
//预览
const fileViewRef = ref()
const showFile = (data) => {
    if(route.path == '/knowledgeRetrieval'){//
        window.pdfViewShow.value = false
    }
    fileViewRef.value.open(data)
}

//文件列表
const searchList = [
    { type: 'input', label: '文件名称', maxlength: 50, prop: 'fileName' }
]

let results = {}
const searchSubmit = result => {
    results = result
    cyFilequeryAllSplit()
}
const searchReset = () => {
    results = {}
    cyFilequeryAllSplit()
}
//列表
const tableData = ref([])
const cyFilequeryAllSplit = () => {
    apis.cyFilequeryAllSplit({
        ...results,
        collectStatus: '1'//1-已收藏 0-未收藏
    }).then(res => {
        if(res.status == 1){
            tableData.value = res.data
        }
    })
}
//下载
const cyFiledownloadById = (row) => {
    apis.cyFiledownloadById({
        id: row.id,
        type: 1
    })
}
//修改（收藏）
const cyFilesaveOne = (row) => {
    apis.cyFilesaveOne({
        id: row.id,
        collectStatus: row.collectStatus == 1 ? 0 : 1
    }).then(res => {
        if(res.status == 1){
            success(res.msg)
            cyFilequeryAllSplit()
            window.Favorites && window.Favorites()//同步其他页面刷新
        }
    })
}
//清空收藏夹
const clearFavorites = () => {
    if(tableData.value.length == 0){
        return
    }
    confirm('确定清空收藏夹吗？', ()=>{
        apis.clearFavorites({}).then(res => {
            if(res.status == 1){
                success(res.msg)
                cyFilequeryAllSplit()
                window.Favorites && window.Favorites()//同步其他页面刷新
            }
        })
    })
}
defineExpose({
    open
})

</script>

<style lang="less" scoped>
:deep(.el-menu-item){
    line-height: 40px;
}
.cont-wrap{
    margin-top:20px;
    height: 420px;
    border-bottom: 1px solid #ccc;
}
.label{
    line-height: 30px;
    text-align: right;
}
:deep(.el-row){
    margin-bottom: 20px;
}
:deep(.search-box.roleCodes){
    .input-box{
        width: 100%;
    }
}
</style>